package main

import (
	"fmt"
	"sync"
)

func main() {
	c := make(chan string)

	var wg sync.WaitGroup

	go func() {
		//time.Sleep(time.Microsecond)
		wg.Add(1)
		for i := 0; i < 10; i++ {
			c <- fmt.Sprintf("协程1-%d",i)
		}
		wg.Done()
	}()

	go func() {
		//time.Sleep(time.Microsecond)
		wg.Add(1)
		for i := 0; i < 10; i++ {
			c <- fmt.Sprintf("协程2-%d",i)
		}
		wg.Done()
	}()

	go func() {
		wg.Wait()
		close(c)
	}()
	//因为add发生在协程内，所以不一定哪个先执行，如果wait先执行，直接就关了，注意和02例子对比
	for n := range c {
		fmt.Println(n)
	}
	//等一下试试
	//time.Sleep(time.Second)
}
